<template>
  <div class="app-container">
    <el-form  class="search-form" :model="queryParams" label-position="top" ref="queryRef"  v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="sickPersonId">
          <el-select style="width:140px" v-model="queryParams.sick_person_id" placeholder="请选择">
            <el-option
              v-for="item in listSickperson"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="手术时间" prop="planTime">
        <el-date-picker clearable
          v-model="queryParams.planTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择计划手术时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="手术单号" prop="sourceId">
        <el-input
          v-model="queryParams.sourceId"
          placeholder="请输入手术申请单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="科室">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入科室ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手术等级">
        <el-input
          v-model="queryParams.asa"
          placeholder="请输入ASA等级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否紧急" prop="isEmergency">
        <el-input
          v-model="queryParams.isEmergency"
          placeholder="请输入紧急状态"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label=".">
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:surgeryplan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:surgeryplan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table class="table-loading" v-loading="loading" :data="surgeryplanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="患者" align="center" prop="sickName" />
      <el-table-column label="计划手术时间" align="center" prop="planTime" width="180">
       
      </el-table-column>
      <el-table-column label="台次" align="center" prop="planSort" />
      <el-table-column label="状态" align="center" prop="status">
        <template>
          
        </template>
      </el-table-column>
      <el-table-column label="手术间ID" align="center" prop="roomName" />
      <el-table-column label="手术室ID" align="center" prop="doorName" />
      <el-table-column label="科室ID" align="center" prop="deptId" />
      <el-table-column label="手术名称" align="center" prop="planName" />
      <el-table-column label="术前诊断" align="center" prop="beforeName" />
      <el-table-column label="麻醉方式" align="center" prop="planAnes" />
      <el-table-column label="手术等级" align="center" prop="surgeryLevel" />
      <el-table-column label="切口类型" align="center" prop="cutType" />
      <el-table-column label="手术部位" align="center" prop="partDesc" />
      <el-table-column label="手术体位" align="center" prop="hisPosture" />
      <el-table-column label="手术分类" align="center" prop="operClass" />
      <el-table-column label="手术类型" align="center" prop="operType" />
      <el-table-column label="手术科室" align="center" prop="implementDept" />
      <el-table-column label="紧急状态" align="center" prop="isEmergency" />
      <el-table-column label="隔离状态" align="center" prop="isQuarantine" />
      <el-table-column label="操作" align="center" class-name="small-padding" width="210px" fixed="right">
        <template #default="scope">
          <div style="display:flex;align-items:center">
            <el-button link type="primary"  @click="handleUpdate(scope.row)" v-hasPermi="['manage:surgeryplan:edit']">修改</el-button>
              <el-button link type="primary"  @click="startPlan(scope.row)" v-hasPermi="['manage:surgeryplan:edit']">开始手术</el-button>
              <el-dropdown @command="moreOperation">
                <span class="el-dropdown-link">
                  更多操作
                  <el-icon class="el-icon--right">
                    <arrow-down />
                  </el-icon>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item>

                    </el-dropdown-item>
                    <el-dropdown-item :command="JSON.stringify({cmd:1,surgeryId:scope.row.surgeryId})">拆台</el-dropdown-item>
                    <el-dropdown-item :command="JSON.stringify({cmd:2,surgeryId:scope.row.surgeryId})">恢复手术</el-dropdown-item>
                    <el-dropdown-item :command="JSON.stringify({cmd:3,surgeryId:scope.row.surgeryId})">取消手术</el-dropdown-item>
                    <el-dropdown-item divided :command="JSON.stringify({cmd:4,surgeryId:scope.row.surgeryId})">不良事件登记</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
          </div>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改手术计划对话框 -->
    <el-dialog :title="title" v-model="open" width="720px" append-to-body>
      <el-form ref="surgeryplanRef" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="患者" prop="sickPersonId">
              <el-select v-model="form.sickPersonId" placeholder="请选择">
                <el-option
                  v-for="item in sickpersonList"
                  :key="item.value"
                  :label="item.sickName"
                  :value="item.sickPersonId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="计划手术时间" prop="planTime">
                <el-date-picker clearable
                  v-model="form.planTime"
                  type="date"
                  value-format="YYYY-MM-DD"
                  placeholder="请选择计划手术时间">
                </el-date-picker>
              </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="台次" prop="planSort">
                <el-input v-model="form.planSort" placeholder="请输入台次" />
              </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="手术申请单号" prop="sourceId">
              <el-input v-model="form.sourceId" placeholder="请输入手术申请单号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手术室" prop="roomId">
                 <el-select v-model="form.roomId" placeholder="请选择">
                    <el-option
                      v-for="item in roomList"
                      :key="item.value"
                      :label="item.deptName"
                      :value="item.deptId">
                    </el-option>
                  </el-select>
              </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手术室" prop="doorId">
              <el-input v-model="form.doorId" placeholder="请输入手术室ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="病区" prop="sickArea">
               <el-select v-model="form.sickArea" placeholder="请选择">
                <el-option
                  v-for="item in sickareaList"
                  :key="item.value"
                  :label="item.sickName"
                  :value="item.sickAreaId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="科室" prop="deptId">
                 <el-select v-model="form.deptId" placeholder="请选择">
                    <el-option
                      v-for="item in deptList"
                      :key="item.value"
                      :label="item.deptName"
                      :value="item.deptId">
                    </el-option>
                  </el-select>
              </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
                 <el-select v-model="form.status" placeholder="请选择">
                    <el-option
                      v-for="item in surgery_plan_status"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
              </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="手术编号" prop="planCode">
                <el-input v-model="form.planCode" placeholder="请输入手术编号" />
              </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="计划手术名称" prop="planName">
              <el-input v-model="form.planName" placeholder="请输入计划手术名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="实施手术名称" prop="applyName">
              <el-input v-model="form.applyName" placeholder="请输入实施手术名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="术前诊断名称" prop="beforeName">
              <el-input v-model="form.beforeName" placeholder="请输入术前诊断名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="术后诊断名称" prop="afterName">
                <el-input v-model="form.afterName" placeholder="请输入术后诊断名称" />
              </el-form-item>   
          </el-col>
          <el-col :span="12">
             <el-form-item label="计划麻醉方式" prop="planAnes">
              <el-input v-model="form.planAnes" placeholder="请输入计划麻醉方式" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="实施麻醉方式" prop="applyAnes">
              <el-input v-model="form.applyAnes" placeholder="请输入实施麻醉方式" />
            </el-form-item> 
          </el-col>
          <el-col :span="12">
             <el-form-item label="手术等级" prop="surgeryLevel">
              <el-input v-model="form.surgeryLevel" placeholder="请输入手术等级" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="手术部位描述" prop="partDesc">
              <el-input v-model="form.partDesc" placeholder="请输入手术部位描述" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="HIS手术医生" prop="hisOperDoctor">
              <el-input v-model="form.hisOperDoctor" placeholder="请输入HIS手术医生" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
               <el-form-item label="HIS麻醉医生" prop="hisAnesDoctor">
                <el-input v-model="form.hisAnesDoctor" placeholder="请输入HIS麻醉医生" />
              </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="入室扫码时间" prop="scanTime">
              <el-input v-model="form.scanTime" placeholder="请输入入室扫码时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="护理预约耗材ID" prop="applyOneoff">
              <el-input v-model="form.applyOneoff" placeholder="请输入护理预约耗材ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手术预约体位" prop="hisPosture">
              <el-input v-model="form.hisPosture" placeholder="请输入HIS手术预约体位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="疼痛评分" prop="painScore">
              <el-input v-model="form.painScore" placeholder="请输入疼痛评分" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="手术准备人" prop="readyBy">
              <el-input v-model="form.readyBy" placeholder="请输入手术准备人" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="手术准备时间" prop="readyTime">
              <el-date-picker clearable
                v-model="form.readyTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择手术准备时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="是否介内镜入手术;0=否  1=是" prop="hopsinfecPci">
              <el-input v-model="form.hopsinfecPci" placeholder="请输入是否介内镜入手术;0=否  1=是" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="术前是否使用抗菌药;0=否  1=是" prop="hopsinfecIsuseanti">
              <el-input v-model="form.hopsinfecIsuseanti" placeholder="请输入术前是否使用抗菌药;0=否  1=是" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
               <el-form-item label="手术分类" prop="operClass">
                <el-input v-model="form.operClass" placeholder="请输入手术分类" />
              </el-form-item>
          </el-col>
          <el-col :span="12">
           <el-form-item label="感染情况" prop="hopsinfecCondition">
            <el-input v-model="form.hopsinfecCondition" placeholder="请输入感染情况" />
          </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="实施手术科室" prop="implementDept">
                <el-input v-model="form.implementDept" placeholder="请输入实施手术科室" />
              </el-form-item> 
          </el-col>
          <el-col :span="12">
            <el-form-item label="登记状态" prop="isRegister">
              <el-input v-model="form.isRegister" placeholder="请输入登记状态" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="手麻手术计划号" prop="aisId">
                <el-input v-model="form.aisId" placeholder="请输入手麻手术计划号" />
              </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="紧急状态" prop="isEmergency">
                <el-input v-model="form.isEmergency" placeholder="请输入紧急状态" />
              </el-form-item> 
          </el-col>
          <el-col :span="12">
              <el-form-item label="隔离状态" prop="isQuarantine">
          <el-input v-model="form.isQuarantine" placeholder="请输入隔离状态" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
             
          </el-col>
          <el-col :span="12">
           
          </el-col>
          <el-col :span="12">
             
          </el-col>
          <el-col :span="12">
           
          </el-col>
          <el-col :span="12">
             
          </el-col>
          <el-col :span="12">
           
          </el-col>
          <el-col :span="12">
             
          </el-col>
          <el-col :span="12">
           
          </el-col>
          <el-col :span="12">
             
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

     <!-- 添加不良事件对话框 -->
    <el-dialog title="添加不良事件" v-model="badeventopen" width="1020px" append-to-body>

      <badeventsurgery :activeSurgery="activeSurgery"></badeventsurgery>
     
    </el-dialog>
  </div>
</template>



<script setup name="Surgeryplan">
import useSurgeryingStore from '@/store/modules/surgerying'
import badeventsurgery from '../badeventsurgery/index.vue'
import { listSurgeryplan, getSurgeryplan, delSurgeryplan, addSurgeryplan, updateSurgeryplan,startSurgery } from "@/api/manage/surgeryplan";
import { listSickperson } from "@/api/manage/sickperson";
import { listSickarea } from "@/api/manage/sickarea";
import { listDept } from "@/api/system/dept";
const router = useRouter();
const { proxy } = getCurrentInstance();
const { surgery_plan_status } = proxy.useDict('surgery_plan_status');
const surgeryplanList = ref([]);
const open = ref(false);
const activeSurgery=ref({});
let badeventopen=ref(false)
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const sickpersonList=ref([])
const roomList=ref([])
let sickareaList=ref([]);
let deptList=ref([]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    sickPersonId: null,
    arrangeFlag: null,
    gotoPacu: null,
    pacuStatus: null,
    planTime: null,
    sourceId: null,
    roomId: null,
    deptId: null,
    asa: null,
    allAmount: null,
    isEmergency: null,
    isQuarantine: null,
  },
  rules: {
    surgeryId: [
      { required: true, message: "自增ID不能为空", trigger: "blur" }
    ],
    sickPersonId: [
      { required: true, message: "患者ID，sick_person表id不能为空", trigger: "blur" }
    ],
    surgeryType: [
      { required: true, message: "手术类型不能为空", trigger: "change" }
    ],
    arrangeFlag: [
      { required: true, message: "排台状态;-1 取消排台，0未排，1已排不能为空", trigger: "blur" }
    ],
    inHospital: [
      { required: true, message: "住院类型不能为空", trigger: "blur" }
    ],
    gotoPacu: [
      { required: true, message: "是否送去pacu，0否1是不能为空", trigger: "blur" }
    ],
    pacuStatus: [
      { required: true, message: "PACU状态不能为空", trigger: "change" }
    ],
    planTime: [
      { required: true, message: "计划手术时间不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询手术计划列表 */
function getList() {
  loading.value = true;
  listSurgeryplan(queryParams.value).then(response => {
    surgeryplanList.value = response.rows;
    surgeryplanList.value=surgeryplanList.value.map(item=>{
      let sick=sickpersonList.value.filter(fitem=>{
        return fitem.sickPersonId==item.sickPersonId
      })[0]
      let room=deptList.value.filter(fitem=>{
        return fitem.deptId==item.roomId
      })[0]
      item.roomName=room?room.roomName:''
      item.sickName=sick?sick.sickName:''
      return item
    })
    total.value = response.total;
    loading.value = false;
  });
}

function moreOperation(item){
  let data=JSON.parse(item)
  activeSurgery.value=data
  switch(Number(data.cmd)){

    case 1:
      break;
    case 2:
      break;
    case 3:
      break;
    case 4:
      badeventopen.value=true
      break;

  }
}

function initData(){
  listSickperson(queryParams.value).then(response => {
    sickpersonList.value = response.rows;
    console.log(sickpersonList.value)
  });

  listSickarea(queryParams.value).then(response => {
    sickareaList.value = response.rows;
  });

  listDept({
    pageNum: 1,
    pageSize: 600
  }).then(response => {
    deptList.value = proxy.handleTree(response.data, "deptId");
  });

  // listDept({
  //   pageNum: 1,
  //   pageSize: 10,
  //   deptType:5
  // }).then(response => {
  //   roomList.value = proxy.handleTree(response.data, "deptId");
  // });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    surgeryId: null,
    sickPersonId: null,
    surgeryType: null,
    hisType: null,
    arrangeFlag: null,
    inHospital: null,
    gotoPacu: null,
    pacuStatus: null,
    planTime: null,
    planSort: null,
    sourceId: null,
    roomId: null,
    doorId: null,
    sickArea: null,
    outSickarea: null,
    deptId: null,
    planCode: null,
    planName: null,
    applyName: null,
    beforeName: null,
    afterName: null,
    inTime: null,
    outTime: null,
    rejectReason: null,
    planAnes: null,
    applyAnes: null,
    patientCondition: null,
    asa: null,
    surgeryLevel: null,
    cutType: null,
    partDesc: null,
    hisOperDoctor: null,
    hisAnesDoctor: null,
    hisTableinNurs: null,
    hisTableoutNurs: null,
    operDoctor: null,
    anesDoctor: null,
    tableinNurs: null,
    tableoutNurs: null,
    hisNumber: null,
    scanTime: null,
    visitDate: null,
    applyOneoff: null,
    hisPosture: null,
    cancelBy: null,
    cancelTime: null,
    painScore: null,
    readyBy: null,
    readyTime: null,
    endBy: null,
    endTime: null,
    isAllergy: null,
    allergy: null,
    reportCount: null,
    gotoId: null,
    gotoDesc: null,
    hopsinfecPci: null,
    hopsinfecIsuseanti: null,
    operClass: null,
    operType: null,
    hopsinfecCondition: null,
    implementDept: null,
    isRegister: null,
    aisId: null,
    allAmount: null,
    pacuStart: null,
    pacuEnd: null,
    pacuId: null,
    isEmergency: null,
    isQuarantine: null,
    pacuReject: null,
    pacuBed: null,
    pacuAnes: null,
    pacuNurs: null,
    pacuShift: null,
    endRemark: null,
    hisRemark: null,
    remark: null,
    status: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("surgeryplanRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.surgeryId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加手术计划";
}

function startPlan(row){
  useSurgeryingStore().setSurgerying(row)
  startSurgery({
    surgeryId:row.surgeryId
  }).then(response=>{
    
  })
  router.push({
    path:'/views/surgerying'
  })
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _surgeryId = row.surgeryId || ids.value
  getSurgeryplan(_surgeryId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改手术计划";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["surgeryplanRef"].validate(valid => {
    if (valid) {
      if (form.value.surgeryId != null) {
        updateSurgeryplan(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSurgeryplan(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _surgeryIds = row.surgeryId || ids.value;
  proxy.$modal.confirm('是否确认删除手术计划编号为"' + _surgeryIds + '"的数据项？').then(function() {
    return delSurgeryplan(_surgeryIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/surgeryplan/export', {
    ...queryParams.value
  }, `surgeryplan_${new Date().getTime()}.xlsx`)
}

getList();
initData();
</script>
<style lang="scss">
  .table-loading{
    .el-loading-mask{
      top:35px!important;
    }
  }
  .search-form{
    display:flex;
    .el-form-item{
      margin-right:5px;
      
    }
  }
</style>
<style lang="scss" scoped>
  .app-container{
    height:100%;
    background:#fff;
  }
</style>
